package com.zz.parkingsystem.controller.inspector;

import com.zz.parkingsystem.dto.aaa;
import com.zz.parkingsystem.entity.Car;
import com.zz.parkingsystem.entity.ExceptionOrder;
import com.zz.parkingsystem.entity.OrderItem;
import com.zz.parkingsystem.service.CarService;
import com.zz.parkingsystem.service.ExceptionOrderService;
import com.zz.parkingsystem.service.Inspector_SectionService;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.Inspector_SectionVO;
import com.zz.parkingsystem.vo.OrderItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zzz
 * @date ：Created in 2022/8/31 21:50
 * @description：aaa
 */
@RestController
@AllArgsConstructor
@RequestMapping("ins/Order")
@Api(value = "巡检员控制层")
@Slf4j
public class OrderItemController {

    private CarService carService;

    private OrderItemService orderItemService;

    private Inspector_SectionService inspector_sectionService;

    private ExceptionOrderService exceptionOrderService;

    //根据泊位号查询车id为空的订单
    @GetMapping("/getUnrecordedVehicle/{pkno}")
    public Result getUnrecordedVehicle(@PathVariable("pkno") int pkno){
        List<OrderItem> unrecordedVehicle = orderItemService.getUnrecordedVehicle(pkno);
        return ResultGenerator.genSuccessResult(unrecordedVehicle);
    }
    // 根据车牌号查订单
    @GetMapping("/carLicensePlates/{carLicensePlate}")
    public Result selectByCarname(@PathVariable("carLicensePlate") String carLicensePlate){
        List<OrderItem> orderItems = orderItemService.selectByCarname(carLicensePlate);
        return ResultGenerator.genSuccessResult(orderItems);
    }

    @ApiOperation(value = "给停车位补充车牌号")
    @PutMapping("/getUnrecorded/{oiNo}/{carLicensePlate}")
    @Transactional(rollbackFor = Exception.class)
    public Result getUnrecorded(@PathVariable("oiNo") String oiNo, @PathVariable("carLicensePlate") String carLicensePlate) {
        //根据订单编号查订单项表
        OrderItemVo unrecorded = orderItemService.getUnrecorded(oiNo);
        log.info(String.format("根据订单编号查 %s", unrecorded));
        //调用添加车辆方法
        Integer userId = unrecorded.getUserId();
        Car car = Car.build(userId, carLicensePlate);
        carService.insertSelective(car);
        //修改订单中的car_id
        OrderItem orderItem = new OrderItem();
        orderItem.setOiId(unrecorded.getOiId());
        orderItem.setCarId(car.getCarId());
        orderItemService.updateByPrimaryKeySelective(orderItem);


        return ResultGenerator.genSuccessResult("添加成功");

    }

    //根据正常订单减去异常订单算出主动支付的订单总数
    //小心表中数据问题
    @GetMapping("/getInitiativeToPay")
    public Result getInitiativeToPay() {
        //总订单
        List<OrderItem> ordersTotal = orderItemService.getOrdersTotal();
        //异常订单总数
        Integer numberOfAbnormal = exceptionOrderService.getNumberOfAbnormal();
        //支付了的订单
        List<OrderItemVo> paymentAmount = orderItemService.getPaymentAmount();
        Integer size = ordersTotal.size();
        Integer size1 = numberOfAbnormal;
        //主动支付的订单总数
        Integer count = paymentAmount.size();
        Integer count3 = 0;
        Integer count4 = 0;
        for (OrderItemVo orderItemVo : paymentAmount) {
            //算微信支付的总额
            if (orderItemVo.getPtType() == 0) {
                Integer oiAmount = orderItemVo.getOiAmount();
                count3 += oiAmount;
            } else {
                //算支付宝支付的总额
                Integer aa = orderItemVo.getOiAmount();
                count4 += aa;
            }
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("coun", count);
        map.put("count3", count3);
        map.put("count4", count4);
        return ResultGenerator.genSuccessResult(map);
    }
}
